package com.zhangsun.system.controller;


import com.zhangsun.base.bean.ResponseData;
import com.zhangsun.common.utils.ResponseUtil;
import com.zhangsun.system.entity.DepartmentEntity;
import com.zhangsun.system.service.DepartmentService;
import com.zhangsun.system.vo.department.DepartmentEditVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author zhangsun
 * @since 2019-08-12
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取部门树
     *
     * @return 部门树
     */
    @GetMapping("/findAll")
    public ResponseData findAll(String deptId) {
        List<DepartmentEntity> departments = departmentService.findAll(deptId);
        return ResponseUtil.success(departments);
    }

    /**
     * 添加部门
     *
     * @param vo 部门
     * @return 结果
     */
    @PostMapping("/create")
    @RequiresPermissions("system:department:edit")
    public ResponseData create(@Validated DepartmentEditVo vo) {
        departmentService.create(vo);
        return ResponseUtil.success();
    }

    /**
     * 获取部门详情
     *
     * @param id 部门id
     * @return 结果
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:department:view")
    public ResponseData findById(@NotBlank @PathVariable String id) {
        DepartmentEntity departmentEntity = departmentService.findById(id);
        return ResponseUtil.success(departmentEntity);
    }

    /**
     * 编辑部门
     *
     * @param id 部门id
     * @param vo 部门
     * @return 结果
     */
    @PutMapping("/update/{id}")
    @RequiresPermissions("system:department:edit")
    public ResponseData update(@NotBlank @PathVariable String id, @Validated DepartmentEditVo vo) {
        departmentService.update(id, vo);
        return ResponseUtil.success();
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return 结果
     */
    @DeleteMapping("/delete/{id}")
    @RequiresPermissions("system:department:delete")
    public ResponseData delete(@NotBlank @PathVariable String id) {
        departmentService.delete(id);
        return ResponseUtil.success();
    }

}
